package com.itheima.advantagesup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.advantagesup.entity.YddArticle;
import com.itheima.advantagesup.mapper.YddArticleMapper;
import com.itheima.advantagesup.service.YddArticleService;
import com.itheima.advantagesup.vo.ArticleVO;
import com.itheima.advantagesup.vo.YddArticlePageBeanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class YddArticleServiceImpl implements YddArticleService {

    @Autowired
    private YddArticleMapper yddArticleMapper;



    @Override
    public YddArticlePageBeanVO list(Long start_time,Long end_time, String title,Integer cateid,Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<YddArticle> list1 = yddArticleMapper.list(start_time,end_time,title,cateid);
        Page<YddArticle> p1 = (Page<YddArticle>) list1;
        List<ArticleVO> list2 = new Page<>();
        for (YddArticle yddArticle : list1) {
            ArticleVO articleVO = yddArticleToArticleVO(yddArticle);
            list2.add(articleVO);
        }
        Page<ArticleVO> p2 = (Page<ArticleVO>) list2;
        YddArticlePageBeanVO yddArticlePageBeanVO = new YddArticlePageBeanVO(p1.getTotal(),p2.getPages(),limit,1,p2.getResult());
        return yddArticlePageBeanVO;
    }

    @Override
    public ArticleVO getArticleById(String id) {
        YddArticle yddArticle = yddArticleMapper.getArticleById(id);
        ArticleVO articleVO = yddArticleToArticleVO(yddArticle);
        return articleVO;
    }

    @Override
    public void delete(String id) {
        yddArticleMapper.delete(id);
    }

    /**
     * 把yddArticle转换为ArticleVO
     * @param yddArticle
     * @return {@link ArticleVO}
     */
    private ArticleVO yddArticleToArticleVO(YddArticle yddArticle) {
        ArticleVO articleVO = new ArticleVO();
        articleVO.setId(yddArticle.getId());
        articleVO.setTitle(yddArticle.getTitle());
        articleVO.setAuthor(yddArticle.getAuthor());
        articleVO.setKeywords(yddArticle.getKeywords());
        articleVO.setDescription(yddArticle.getDescription());
        articleVO.setContent(yddArticle.getContent());
        articleVO.setPic(yddArticle.getPic());
        articleVO.setClick(yddArticle.getClick());
        articleVO.setIshot(yddArticle.getIshot());
        articleVO.setIstop(yddArticle.getIstop());
        articleVO.setCreate_date(yddArticle.getCreateDate());
        articleVO.setUpdate_date(yddArticle.getUpdateDate());
        articleVO.setCateid(yddArticle.getCateid());
        articleVO.setTags(yddArticle.getTags());
        articleVO.setStatus(yddArticle.getStatus());
        articleVO.setLovenum(yddArticle.getLovenum());
        articleVO.setUser_id(yddArticle.getUserId());
        articleVO.setDefault_data(yddArticle.getDefaultData());
        articleVO.setCatename(yddArticle.getCatename());
        return articleVO;
        
    }
}
